use shq_common::prelude::get_my_pool;
use sqlx::Row;
/// 创建部门审批人
pub async fn create_dept_approvers(
    ny: u32,
    wfid: u32,
    emid: u32,
    dpids: Vec<u32>,
) -> anyhow::Result<()> {
    let pool = get_my_pool();
    let mut tr = pool.begin().await?;
    let sql = "delete from hr_dept_approvers where ny = ? and wfid = ? and emid = ?";
    sqlx::query(sql)
        .bind(ny)
        .bind(wfid)
        .bind(emid)
        .execute(&mut *tr)
        .await?;
    for dpid in dpids {
        let sql = "insert into hr_dept_approvers(ny,wfid,dpid,emid) values (?,?,?,?)";
        sqlx::query(sql)
            .bind(ny)
            .bind(wfid)
            .bind(dpid)
            .bind(emid)
            .execute(&mut *tr)
            .await?;
    }
    tr.commit().await?;
    Ok(())
}
/// 创建信贷报告流程
pub async fn create_credit_report_flow(
    ny: u32,
    create_emid: u32,
    emids: Vec<u32>,
) -> anyhow::Result<()> {
    let wfid = 1;
    let pool = get_my_pool();
    let mut tr = pool.begin().await?;
    build_flow_create_log(ny, wfid, create_emid, &mut tr).await?;
    for emid in emids {
        let mut ec = shq_common::prelude::empl::EmployeeCache::new()?;
        let empl = ec.get_empl_by_id(emid).await?;
        let dept = ec.get_dept_by_id(empl.organ_id)?;
        let emno: String = empl.no;
        let emnm: String = empl.name;
        let dpid: u32 = dept.id;
        let dpnm: String = dept.name;
        let frid = shq_workflow::create(wfid, emid).await?;
        let sql = "insert into hr_credit_report_run(ny,frid,wfid,emid,emno,emnm,dpid,dpnm,file_id,is_bccl) values (?,?,?,?,?,?,?,?,?,?)";
        sqlx::query(sql)
            .bind(ny)
            .bind(frid)
            .bind(wfid)
            .bind(emid)
            .bind(emno)
            .bind(emnm)
            .bind(dpid)
            .bind(dpnm)
            .bind(0)
            .bind(0)
            .execute(&mut *tr)
            .await?;
    }
    tr.commit().await?;
    Ok(())
}

/// 创建交易流水流程
pub async fn create_transaction_flow(
    ny: u32,
    create_emid: u32,
    emids: Vec<u32>,
) -> anyhow::Result<()> {
    let wfid = 2;
    let pool = get_my_pool();
    let mut tr = pool.begin().await?;
    build_flow_create_log(ny, wfid, create_emid, &mut tr).await?;
    for emid in emids {
        let mut ec = shq_common::prelude::empl::EmployeeCache::new()?;
        let empl = ec.get_empl_by_id(emid).await?;
        let dept = ec.get_dept_by_id(empl.organ_id)?;
        let emno: String = empl.no;
        let emnm: String = empl.name;
        let dpid: u32 = dept.id;
        let dpnm: String = dept.name;
        let frid = shq_workflow::create(wfid, emid).await?;
        let sql = "insert into hr_transaction_run(ny,frid,wfid,emid,emno,emnm,dpid,dpnm,is_bccl) values (?,?,?,?,?,?,?,?,?)";
        sqlx::query(sql)
            .bind(ny)
            .bind(frid)
            .bind(wfid)
            .bind(emid)
            .bind(emno)
            .bind(emnm)
            .bind(dpid)
            .bind(dpnm)
            .bind(0)
            .execute(&mut *tr)
            .await?;
    }
    tr.commit().await?;
    Ok(())
}

/// 创建家访流程
pub async fn create_home_visit_flow(
    ny: u32,
    create_emid: u32,
    dpids: Vec<u32>,
) -> anyhow::Result<()> {
    let wfid = 3;
    let pool = get_my_pool();
    let mut tr = pool.begin().await?;
    build_flow_create_log(ny, wfid, create_emid, &mut tr).await?;
    for dpid in dpids {
        let uids = shq_auth::get_role_users_by_organ(11, dpid).await?;
        let mut dc = shq_common::prelude::dept::DeptmentCache::new()?;
        let dtpt = dc.get_dept_by_id(dpid).await?;
        if uids.is_empty() {
            anyhow::bail!("部门{}没有设置员工行为管理复核员", dtpt.name);
        }
        let emid = uids[0];
        let frid = shq_workflow::create(wfid, emid).await?;
        let dpnm: String = dtpt.name;
        let sql = "insert into hr_home_visit_run(ny,frid,wfid,dpid,dpnm) values (?,?,?,?,?)";
        sqlx::query(sql)
            .bind(ny)
            .bind(frid)
            .bind(wfid)
            .bind(dpid)
            .bind(dpnm)
            .execute(&mut *tr)
            .await?;
    }
    tr.commit().await?;
    Ok(())
}

async fn build_flow_create_log(
    ny: u32,
    wfid: u32,
    create_emid: u32,
    tr: &mut sqlx::Transaction<'_, sqlx::MySql>,
) -> anyhow::Result<()> {
    let sql = "select count(*) from hr_flow_create_log where ny = ? and wfid = ?";
    let row = sqlx::query(sql)
        .bind(ny)
        .bind(wfid)
        .fetch_one(&mut **tr)
        .await?;
    let count: i64 = row.try_get(0)?;
    if count > 0 {
        return Err(anyhow::anyhow!("流程创建记录已存在，不能重复创建"));
    }
    let sql =
        "insert into hr_flow_create_log(ny,wfid,create_emid,create_time) values (?,?,?,now())";
    sqlx::query(sql)
        .bind(ny)
        .bind(wfid)
        .bind(create_emid)
        .execute(&mut **tr)
        .await?;
    Ok(())
}
